﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class ProductController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IProductRepository ProductRepository;
        private readonly IMaterialRepository materialRepository;
        private readonly IProductTypeRepository producTypeRepository;
        private readonly IUOMRepository uomRepository;
        private readonly ICategoryRepository cateRepository;
        public ProductController(ICommandBus commandBus, IProductRepository ProductRepository, IMaterialRepository materialRepository, IProductTypeRepository producTypeRepository, IUOMRepository uomRepository, ICategoryRepository cateRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.ProductRepository = ProductRepository;
            this.cateRepository = cateRepository;
            this.materialRepository = materialRepository;
            this.producTypeRepository=producTypeRepository;
            this.uomRepository=uomRepository;
        }
        //
        // GET: /Product/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = ProductRepository.GetAll();
            ViewData["ProductType"] = producTypeRepository.GetAll();
            ViewData["Product"] = ProductRepository.GetAll();
            ViewData["Category"] = cateRepository.GetAll();
            ViewData["UOM"] = uomRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(ProductFormModels frm)
        {
            var form = new ProductFormModels()
            {
                ID = frm.ID,
                Attachment=frm.Attachment,
                CategoryID=frm.CategoryID,
                Code=frm.Code,
                Description=frm.Description,
                Name=frm.Name,
                ProductTypeID=frm.ProductTypeID,
                ImageDesign=frm.ImageDesign,
                Level=frm.Level,
                ParentID=frm.ParentID,
               
                
            };
            var command = Mapper.Map<ProductFormModels, CreateOrUpdateProductCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = ProductRepository.GetAll();
            return PartialView("_ProductList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            Product product = ProductRepository.GetById(ID);
            //Dung de xu ly loi circual deteted
            var obj = Mapper.Map<Product, ProductFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteProductCommand> list = new List<DeleteProductCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteProductCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = ProductRepository.GetAll();
            return PartialView("_ProductList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Product> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = ProductRepository.GetAll();
            }
            else
            {
                model = ProductRepository.GetMany(x => (x.Name.Contains(Name)) || (x.Description.Contains(Name)));

            }
            return PartialView("_ProductList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = ProductRepository.GetAll();
            return PartialView("_ProductList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
